package com.easy.project.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.project.config.file.FileConfig;
import com.easy.project.domain.dto.common.SysFileDTO;
import com.easy.project.domain.po.system.SysFilePO;
import com.easy.project.domain.vo.common.SysFileVO;
import com.easy.project.mapper.system.SysFileMapper;
import com.easy.project.service.common.ISysFileService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 策略类实现不同的实现调用
 * </p>
 *
 * @author 
 * @since 2024-11-06
 */
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> implements ISysFileService {

    @Resource
    @Lazy
    private List<ISysFileService> sysFileServiceList;

    @Resource
    private FileConfig fileConfig;

    @Override
    public String getType() {
        return null;
    }

    @Override
    public SysFileVO upload(MultipartFile file) {
        for (ISysFileService sysFileService : sysFileServiceList){
            if (Objects.equals(sysFileService.getType(),fileConfig.getType())){
                return sysFileService.upload(file);
            }
        }
        return null;
    }

    @Override
    public List<SysFileVO> getUrlList(SysFileDTO sysFileDTO) {
        for (ISysFileService sysFileService : sysFileServiceList){
            if (Objects.equals(sysFileService.getType(),fileConfig.getType())){
                return sysFileService.getUrlList(sysFileDTO);
            }
        }
        return null;
    }

    @Override
    public void download(String fileKey, HttpServletResponse response) {
        for (ISysFileService sysFileService : sysFileServiceList){
            // 做到无缝切换文件支持不同类型的文件下载
            SysFilePO sysFilePO = getOne(new LambdaQueryWrapper<SysFilePO>().eq(SysFilePO::getFileKey, fileKey));
            if (Objects.equals(sysFilePO.getFileType(),fileConfig.getType())){
                sysFileService.download(fileKey,response);
            }
        }
    }

}
